/*  $('.btn').on('click', function() {
    window.location.href = rootdomain + 'html/product-order/payment.html'
  })*/
$('.head-img').on('click', function() {
  window.history.go(-1);
})
/* 数据回显 */
var data = sessionStorage.getItem("xdMsg");
data = JSON.parse(data);
var cardType;
data.clubcardType == 1 ? cardType = "计次卡" : cardType = "周期卡";
console.log(data.type + "----" + cardType)
data.clubcardType == 1 ? clubtitle = "打卡次数" : clubtitle = "有效天数";
data.type
$("#play_time span").html(data.time)
$("#clubtitle").html(clubtitle);
$("#cardType").html(cardType);
$("#clubcardId").html(data.clubcardId);
$("#price").html((data.price * 1).toFixed(2));
$("#payPrice").html((data.price * 1).toFixed(2));
/* 选择优惠券 */
$("#coupon").click(function() {
  var ua = navigator.userAgent.toLowerCase();
  if (/pjyc_i/.test(ua)) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler('jsToClientCoupon', function(resp) {});
    });
  } else if (/pujiyicheng/.test(ua)) {
    window.stub.jsToClientCoupon(); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

  }
})
//ios获取优惠券id
if (mark == 1) {
  setupWebViewJavascriptBridge(function(bridge) {
    bridge.registerHandler('getCardId', function(res) {
      //tips(id)
      if (typeof(res) == "string") {
        res = JSON.parse(res)
      }
      $("#coupon").html(res.money)
      data.couponId = res.couponId;
      var price = $("#payPrice").html();
      price -= res.money;
      $("#payPrice").html(price > 0 ? price : "0.00")
    });
  });
}
//android获取优惠券id
function getCardId(res) {
  res = JSON.parse(res)
  $("#coupon").html(res.price).addClass("coupon")
  data.couponId = res.id;
  var price = $("#payPrice").html();
  price -= res.price;
  $("#payPrice").html(price > 0 ? price : "0.00")
  //var result = window.stub.clientToJsCid();
}
/* 手机号输入校验 */
$("#user_phone").on("input", function() {
  this.value = this.value.replace(/\D/, "");
})
/* 提交订单 */
$("#submit_order").click(function() {
  submitOrder(this)
})

function submitOrder(self) {
  var link_name = $("#link_name").val();
  var link_phone = $("#link_phone").val();
  if (!link_name) {
    tips("请填写联系人");
    return;
  }
  if (!link_phone) {
    tips("请填写联系电话");
    return;
  }
  if (link_phone.length < 11) {
    tips("请检查联系电话是否正确");
    return;
  }
  if ($(self).hasClass("disabled")) {
    return false;
  }
  if (Auth()) {
    $(self).addClass("disabled");
    var dataObj = {
      id: user_info.id,
      phone: user_info.phone,
      uuid: user_info.uuid,
      clubcardId: data.cardId,
      couponId: data.couponId,
      type: data.type,
      clubcardType: data.clubcardType,
      price: $("#payPrice").html(),
      linkName: link_name,
      linkPhone: link_phone,
      userType: mark,
      beginTime: data.time.split("至")[0],
      endTime: data.time.split("至")[1]
    }
    $.ajax({
      type: "post",
      url: apicom + "mobile/after/card/order/createServiceOrder",
      data: dataObj,
      dataType: "json",
      success: function(res) {
        if (typeof(res) == 'string') {
          res = JSON.parse(res);
        }
        setTimeout(function() {
          $(self).removeClass("disabled");
        }, 2000)
        if (res.code == 1) {
          var createdTime = res.content.createdTime;
          var v = res.content.orderNum;
          if (res.content.payType == 2) {
            locationReplace(rootdomain + "html/service/complete.html?v=" + v + "&oId=" + res.content.orderId)
          } else {
            locationReplace(rootdomain + "html/service/topay.html?t=" + createdTime + "&p=" + dataObj.price + "&v=" + v + "&id=" + res.content.orderId + "&type=1")
          }
          //window.location.href = rootdomain + "html/service/topay.html?t=" + createdTime + "&p=" + dataObj.price + "&v=" + v + "&id=" + res.content.orderId + "&type=2";

        } else {
          tips(res.msg)
        }
      },
      error: function(err) {
        $(self).removeClass("disabled");
        console.log(err)
      }
    })
  } else {
    sessionStorage.setItem('history', window.location.href);
    toClientLogin(location.href)
  }
}
/* 替换当前页面url */
function locationReplace(url) {
  if (history.replaceState) {
    history.replaceState(null, document.title, url);
    history.go(0);
  } else {
    location.replace(url);
  }
}